<template>
  <div class="container">
    <img src="../../assets/logo.png">
    <!-- <h1>A Data Analysis Board in Vue.</h1> -->
    <el-form class="form-wrapper">
      <el-form-item>
        <el-input v-model="user.username" type="text" :placeholder=" $t('auth.usernamePlaceholder')" />
      </el-form-item>
      <el-form-item>
        <el-input v-model="user.password" type="password" :placeholder=" $t('auth.passwordPlaceholder')" />
      </el-form-item>
      <el-row>
        <el-col :span="16">
          <el-form-item>
            <el-input v-model="user.inputCode" type="text" :placeholder=" $t('auth.verificationCodePlaceholder')" />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="text-align: right;">
          <img v-if="requestCodeSuccess" style="margin-top: 2px;" :src="randCodeImage" @click="handleChangeCheckCode"/>
          <img v-else style="margin-top: 2px;" src="../../assets/checkcode.png" @click="handleChangeCheckCode"/>
        </el-col>
      </el-row>
      
      <el-button :loading="loading" type="primary" @click="handleLogin">
        {{ $t('auth.signIn') }}
      </el-button>
      <!-- <el-divider />
      <div style="display: flex;">
        <el-button type="text" size="mini" style="text-align:left;" @click="useTestAccount">
          {{ $t('auth.useTestAccount') }}
        </el-button>
        <el-button type="text" size="mini" style="text-align:right;" @click="$router.push('/signup')">
          {{ $t('auth.toSignUp') }}<i class="el-icon-right" />
        </el-button>
      </div> -->
    </el-form>
  </div>
</template>
<script>
import { login,getCheckCode } from '@/api/user'
export default {
  data() {
    return {
      loading: false,
      user: {
        username: '',
        password: '',
        inputCode:''
      },
      requestCodeSuccess:false,//验证码图显示隐藏
      randCodeImage:'',//验证码图片地址
      currdatetime:''
    }
  },
  created(){
    this.handleChangeCheckCode(); //获取验证码
  },
  methods: {
    handleChangeCheckCode(){
      this.currdatetime = new Date().getTime();
      getCheckCode(this.currdatetime).then(res =>{
        if(res.success){
          this.randCodeImage = res.result
          this.requestCodeSuccess=true
        }else{
          this.requestCodeSuccess=false
        }
      }).catch((err)=>{
        this.requestCodeSuccess=false
      })
    },
    handleLogin() {
      this.loading = true
      let loginParams ={}
      loginParams.username = this.user.username
      loginParams.password = this.user.password
      loginParams.captcha =  this.user.inputCode
      loginParams.checkKey = this.currdatetime
      login(loginParams).then(resp => {
        this.loading = false
        this.$store.dispatch('user/Login',resp.result.token).then(() => {
          this.$router.push(this.$route.query.redirect || '/')
        })
      }).catch(() => {
        this.loading = false
      })
    },
    useTestAccount() {
      this.user = {
        userName: 'dongsuo',
        password: '123456'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  text-align: center;
  padding-top: 150px;
}
.form-wrapper {
  width: 350px;
  margin: 0 auto;
  padding-top: 40px;
  .el-button {
    width: 100%;
  }
}
</style>
